package com.dragon.logical.service.supplies.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.supplies.IProductUnitService;
import com.dragon.repository.entity.ProductUnit;
import com.dragon.repository.mapper.ProductUnitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 产品单位 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-09-18
 */
@Service
public class ProductUnitServiceImpl extends ServiceImpl<ProductUnitMapper, ProductUnit> implements IProductUnitService {

    @Autowired
    ProductUnitMapper unitMapper;

    @Override
    public Page<ProductUnit> getList(Integer page, Integer limit, Integer state) {
        //查询列表
        Page<ProductUnit> unitPage = new Page<>(page,limit);
        QueryWrapper<ProductUnit> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(state != null , ProductUnit::getState,state);
        Page<ProductUnit> productUnits = unitMapper.selectPage(unitPage,wrapper);
        return productUnits;
    }

    @Override
    public void addUnit(ProductUnit productUnit) {
        unitMapper.insert(productUnit);
    }

    @Override
    public void updateUnit(ProductUnit productUnit) {
        unitMapper.updateById(productUnit);
    }

    @Override
    public void deleteUnit(Integer id) {
        ProductUnit productUnit = unitMapper.selectById(id);
        if (productUnit.getState() == 1){
            throw new CustomException(ResultCode.PRODUCT_UNIT_IS_ENABLE);
        }
        unitMapper.deleteById(id);
    }
}
